/*************************************************************
 *Name:           Christopher Alexander
 *Email address:  alexanc2@onid.oregonstate.edu
 *Class name:     CS362
 *Assignment:     Assignment #3
 *Description:    This file (unittest4.c) contains unit tests
                  for gainCard() in dominion.c
 *************************************************************/

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define NUM_UNIQUE_CARDS 27 //At least by my count this is right.


/*This function is used to generate a gamestate for preState that
 *should be identical to the state after gainCard() is called.*/
int preGainCard(int supplyPos, struct gameState *preState, int toFlag, int player) {

   if (supplyCount(supplyPos, preState) < 1){
      return -1;
   }
   if (toFlag == 0){
      preState->discard[player][ preState->discardCount[player] ] = supplyPos;
      preState->discardCount[player]++;
   }
   if (toFlag == 1){
      preState->deck[ player ][ preState->deckCount[player] ] = supplyPos;
      preState->deckCount[player]++;
   }
   if (toFlag == 2){
      preState->hand[ player ][ preState->handCount[player] ] = supplyPos;
      preState->handCount[player]++;
   }
   preState->supplyCount[supplyPos]--;

   return 0;
}

/*This function is used check the accuracy of gainCard()*/
int checkGainCard(int supplyPos, struct gameState *testState, int toFlag, int player) {

   struct gameState preState;

   memcpy(&preState, testState, sizeof(struct gameState));

   if (preGainCard(supplyPos, &preState, toFlag, player) !=
       gainCard(supplyPos, testState, toFlag, player)){
      printf("\tBUG FOUND RUNNING gainCard()\n"
             "\t\tReturn values do not match.\n");
   }

   if (memcmp(&preState, testState, sizeof(struct gameState)) != 0){
      printf("\tBUG FOUND RUNNING gainCard()\n");
   }

   return 0;
}


int main() {
   int i, n, player, toFlag, supplyPos;
   struct gameState G;
   int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
   int seed;
   int numPlayer = 2;

   printf("\nTesting gaincard():\n");

   for (n = 0; n < 2000; n++) {
      for (i = 0; i < sizeof(struct gameState); i++){
         ((char*)&G)[i] = (int) (Random() * 256);
      }
      seed = (int) (Random() * 1000);
      initializeGame(numPlayer, k, seed, &G);

      //Randomly assign valid amounts to gainCard parameters.
      toFlag = (int) (Random() * 3);
      supplyPos = k[ ((int)(Random() * 10)) ];
      player = (int) (Random() * 2);

      checkGainCard(supplyPos, &G, toFlag, player);
   }
    return 0;
}
